<template>
  <div class="login">
    <div class="main" >
      <img src="../../../static/img/logo/logo.png" alt="" class="img1">
      <img src="../../../static/img/backgroung/img_bg.png" alt="" class="img2">
      <h3>劳务工资代发系统</h3>
      <h4>Labor Wage Agency System</h4>
      <!-- <p style="font-size:20px;margin-left:10px;font-weight:600">Labor Management System of Wages Distribution Agency Service</p> -->
      <el-form ref="loginForm" 
        :model="loginForm" 
        :rules="loginRules" 
        class="login-form" 
        auto-complete="on" 
        label-position="left"
      >
        <h2 class="title">欢迎登录</h2>
        <el-form-item prop="username" >
          <el-input v-model="loginForm.username" 
            name="username" type="text" 
            @focus="isUsername=true"
            @blur="isUsername=false"
            auto-complete="on" 
            placeholder="请输入用户名" 
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="user" v-bind:class="{'color-class':isUsername}" class=" input-icon " />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" >
          <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="on"
            placeholder="请输入登录密码"
            @focus="isPassword=true"
            @blur="isPassword=false"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" v-bind:class="{'color-class':isPassword}" class="el-input__icon input-icon " />
          </el-input>
        </el-form-item>
        <el-form-item prop="checkcode">
          <el-input v-model="code" 
            tabindex="3" 
            name="checkcode" 
            type="text" 
            auto-complete="on" 
            maxlength="4"
            placeholder="请输入验证码"
            @focus="isValidCode=true"
            @blur="isValidCode=false"
            @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="validCode" v-bind:class="{'color-class':isValidCode}" class="el-input__icon input-icon "  />
          </el-input>
          <div class="login-code" @click="refreshCode">
            <s-identify :identifyCode="identifyCode"></s-identify>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button :loading="loading" type="primary" style="width:100%;background:#1ABC9C;border:0px;border-radius:5px;height:40px;margin-top:20px" @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>© 2021 All Rights Reserved. 昊广智联<br> 鄂ICP备1234567890号 <img src="" alt=""></span>
    </div>
  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import SIdentify from '@/components/sidentify/sidentify'

export default {
  components:{SIdentify},
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入密码'))
      }else if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    const validateCheckcode = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('请输入正确的验证码'))
      } else {
        callback()
      }
    }
    return {
      isUsername:false,
      isPassword:false,
      isValidCode:false,
      identifyCodes: "1234567890",
      identifyCode: "",
      code: '', //text框输入的验证码
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        checkcode:[{ required: true, trigger: 'blur', validator: validateCheckcode }]
      },
      loading: false,
      pwdType: 'password',
      redirect: undefined
    }
  },
  mounted(){
     this.identifyCode = ""
     this.makeCode(this.identifyCodes, 4)
  },
  created() {
    this.refreshCode()
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      localStorage.clear()
      this.$refs.loginForm.validate(valid => {
        if (this.code == '') {
          this.$message({
            message: '请输入验证码！',
            type: 'warning',
          })
          return
        }
        if (this.identifyCode !== this.code) {
          this.code = ''
          this.refreshCode()
          this.$message({
            message: '请输入正确的验证码！',
            type: 'warning',
          })
          return
        }

        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false       
            this.$router.push({ path: this.redirect || '/home' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
          
    refreshCode() {
        this.identifyCode = "";
        this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
          this.identifyCode += this.identifyCodes[
            this.randomNum(0, this.identifyCodes.length)
          ];
      }
      console.log(this.identifyCode)
      // this.code = this.identifyCode
  },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#e6e6e6;
$dark_gray:#7f7f7f;
$light_gray:#7f7f7f;

/*验证码样式*/
.login-code {
  cursor: pointer;
  position: absolute;
  right: 3px;
  top: 0px;
}

.login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-color: #0D1D30;
    position: relative;
  }
.main{
    position: relative;
    width: 940px;
    height: 495px;
    background:rgba(25,41,59,1);
    border-radius:12px;
    box-shadow:0px 2px 12px 0px rgba(9,23,39,1);
    color:rgba(255,255,255,1);
    .img1{
      position:absolute;
      top: 46px;
      left: 53px;
      width: 110px;
    }
    .img2{
      position:absolute;
      top: 63px;
      left: 132px;
      width: 348px;
      height: 299px;
    }
    h3{
      margin-left: 52px;
      margin-top: 383px;
      margin-bottom: 0;
      font-size:32px;

    }
    h4{
      margin-left: 52px;
      font-size:24px;
      margin-top: 16px;
    }
    .login-form {
      position:absolute;
      right: 0;
      top: 0;
      border-radius: 6px;
      background: #ffffff;
      width: 299px;
      height: 495px;
      padding: 58px 33px 33px 33px;
      .title {
        width:85px;
        font-size:20px;
        margin: 0px auto 62px auto;
        border-bottom: 3px rgba(27,74,127,1) ridge;
        text-align: center;
        color: #333333;
        padding-bottom: 8px;
      }
      // .el-input {
      //   height: 32px;
      //   // border: 0 !important;
      //   input:focus  {
      //     color:rgba(6,104,185,1);
      //   }
      //   input {
      //     outline:none;
      //     height: 38px;
      //     border-top: 0 !important;
      //     border-left: 0 !important;
      //     border-right: 0 !important;
      //     border-radius:0 !important;
      //   }

      // }
      .color-class{
        color:rgba(6,104,185,1);
      }
      .input-icon {
        height: 39px;
        width: 19px;
        margin-left: 2px;
      }
    }
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: absolute;
    transform: translateY(273px);
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
    span{
      img{
        height: 12px;
      }
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss">
  .login-form {
    .el-input {
        height: 32px;
        // border: 0 !important;
        input:focus  {
          color:rgb(70, 70, 70);
          // color: yellow;
        }
        input {
          outline:none;
          height: 38px;
          border-top: 0 !important;
          border-left: 0 !important;
          border-right: 0 !important;
          border-radius:0;
        }

      }
  }
  
</style>